/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.app.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.app.entity.Goods2;
import com.thinkgem.jeesite.modules.app.service.Goods2Service;

/**
 * 商品Controller
 * @author dby
 * @version 2020-07-14
 */
@Controller
@RequestMapping(value = "${adminPath}/app/goods2")
public class Goods2Controller extends BaseController {

	@Autowired
	private Goods2Service goods2Service;
	
	@ModelAttribute
	public Goods2 get(@RequestParam(required=false) String id) {
		Goods2 entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = goods2Service.get(id);
		}
		if (entity == null){
			entity = new Goods2();
		}
		return entity;
	}
	
	@RequiresPermissions("app:goods2:view")
	@RequestMapping(value = {"list", ""})
	public String list(Goods2 goods2, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Goods2> page = goods2Service.findPage(new Page<Goods2>(request, response), goods2); 
		model.addAttribute("page", page);
		return "modules/app/goods2List";
	}

	@RequiresPermissions("app:goods2:view")
	@RequestMapping(value = "form")
	public String form(Goods2 goods2, Model model) {
		model.addAttribute("goods2", goods2);
		return "modules/app/goods2Form";
	}

	@RequiresPermissions("app:goods2:edit")
	@RequestMapping(value = "save")
	public String save(Goods2 goods2, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, goods2)){
			return form(goods2, model);
		}
		goods2Service.save(goods2);
		addMessage(redirectAttributes, "保存商品成功");
		return "redirect:"+Global.getAdminPath()+"/app/goods2/?repage";
	}
	
	@RequiresPermissions("app:goods2:edit")
	@RequestMapping(value = "delete")
	public String delete(Goods2 goods2, RedirectAttributes redirectAttributes) {
		goods2Service.delete(goods2);
		addMessage(redirectAttributes, "删除商品成功");
		return "redirect:"+Global.getAdminPath()+"/app/goods2/?repage";
	}


	@RequiresPermissions("app:goods2:view")
	@RequestMapping(value = {"index"})
	public String index(Goods2 goods2, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Goods2> page = goods2Service.findPage(new Page<Goods2>(request, response), goods2);
		model.addAttribute("page", page);
		return "modules/app/index";
	}

	@RequiresPermissions("app:goods2:view")
	@RequestMapping(value = "view")
	public String view(Goods2 goods2, Model model) {
		model.addAttribute("goods2", goods2);
		return "modules/app/goods2View";
	}



}